package StudentSystem;

import java.io.*;
import java.util.Scanner;

public class Main {
    public static void main(String[] args) throws IOException {
        Scanner sc = new Scanner(System.in);
        Student[] students = new Student[5];
        int num = 0;
        int choice = 0;
        while(choice != 7){
            menu();
            choice = sc.nextInt();
           switch (choice){
               case 1:
                   num = addStudent(students,num);
                   break;
               case 2:
                   num = remove(students,num);
                   break;
               case 3:
                   fix(students,num);
                   break;
               case 4:
                   find(students,num);
                   break;
               case 5:
                   num = input(students,num);
                   break;
               case 6:
                   outPut(students,num);
                   break;
               case 7:
                   break;
               default:
               System.out.println("输入错误，请重新输入：");
                   break;
           }
        }
    }
    public static void menu(){
        System.out.println("---------学生管理系统-----------");
        System.out.println("一：添加学生信息");
        System.out.println("二：删除学生信息");
        System.out.println("三：修改学生信息");
        System.out.println("四：查询学生信息");
        System.out.println("五：录入学生信息");
        System.out.println("六：导出学生信息");
        System.out.println("七：退出学生管理系统");
    }
    public static int addStudent(Student[] students,int num){
        Scanner sc = new Scanner(System.in);
        while (true){
            if(num == students.length){
                students = addContent(students);
            }
            System.out.println("请依次输入学生的学号、姓名、年龄、家庭住址（以空格分隔）");
            students[num] = new Student(sc.nextInt(),sc.next(),sc.nextInt(),sc.next());
            num++;
            System.out.println("学生信息已被添加");
            System.out.println("是否继续添加：");
            if(!sc.nextBoolean()){
                return num;
            }
        }
    }
    public static int remove(Student[] students,int num){
        Scanner sc = new Scanner(System.in);
        while(true){
            if(num<=0){
                System.out.println("无可删除的学生信息");
                return num;
            }
            System.out.println("请输入想要删除的学生的学号：");
            int id = sc.nextInt();
            int index = findStudent(id, num, students);
            if(index == -1){
                System.out.println("未找到该学生信息");
            }
            if(index != num-1 && index!=-1){
                for (int i = index; i < num-1; i++) {
                    students[i] = students[i+1];
                }
                System.out.println("该学生信息已被删除");
            }
            num--;
            System.out.println("是否继续删除：");
            if(!sc.nextBoolean()){
               return num;
            }
        }
    }
    public static int findStudent(int id,int num,Student[] students){
        for (int i = 0; i < num; i++) {
            if(students[i].getId() == id){
                return i;
            }
        }
        return -1;
    }
    public static void fix(Student[] students,int num){
        Scanner sc = new Scanner(System.in);
        while(true){
            System.out.println("请输入想要修改的学生的学号：");
            int id = sc.nextInt();
            int index = findStudent(id,num,students);
            if(index == -1){
                System.out.println("未找到该学生信息");
            }
            System.out.println("请输入想要修改的部分：");
            System.out.println("1：姓名");
            System.out.println("2：家庭住址");
            System.out.println("3：年龄");
            switch(sc.nextInt()){
                case 1:
                    System.out.println("请输入新的名字");
                    students[index].setName(sc.nextLine());
                    break;
                case 2:
                    System.out.println("请输入新的家庭住址");
                    students[index].setAddress(sc.nextLine());
                    break;
                case 3:
                    System.out.println("请输入新的年龄");
                    students[index].setAge(sc.nextInt());
                    break;
            }
            System.out.println("是否继续修改：");
            if(!sc.nextBoolean()){
                break;
            }
        }
    }
    public static int input(Student[] students,int num) throws IOException {
        BufferedReader br = new BufferedReader(new FileReader(new File("C:\\Users\\和靖皓\\Desktop\\学生信息系统.txt")));
        String b;
        while((b = br.readLine())!=null){
            Student s = new Student();
            if(num == students.length-1){
                addContent(students);
            }
            //名字 学号 年龄 家庭住址
            System.out.println(b);
            s.setName(b.split(" ")[0]);
            s.setId(Integer.parseInt(b.split(" ")[1]));
            s.setAge(Integer.parseInt(b.split(" ")[2]));
            s.setAddress(b.split(" ")[3]);
            students[num] = s;
            num++;
        }
        System.out.println("已经完成录入");
        br.close();
        return num;
    }
    public static void outPut(Student[] students,int num) throws IOException {
        BufferedWriter bw = new BufferedWriter(new FileWriter(new File("C:\\Users\\和靖皓\\Desktop\\学生信息系统备份.txt")));
        if(num<=0){
            System.out.println("无可打印的学生信息");
        }
        //BufferedWriter的write方法会把文本和字符串直接打印，而数字则会打印Unicode对应的字符
        //所以如果想要打印数字，则需要先把它转为字符串然后再转为int再write
        for (int i = 0; i < num; i++) {
            bw.write(students[i].getName() + " " +
                    students[i].getId() + " " +
                    students[i].getAge() + " " +
                    students[i].getAddress());
        }
        bw.close();
        System.out.println("学生的信息已打印到文件中");
    }
    public static void find(Student[] students,int num){
        Scanner sc = new Scanner(System.in);
        while(true){
            System.out.println("请输入想要查找的学生的学号：");
            int id = sc.nextInt();
            int index = findStudent(id,num,students);
            if(index == -1){
                System.out.println("未找到该学生信息");
            }else{
                System.out.println("该学生的姓名是："+students[index].getName()+"，年龄是："+students[index].getAge()+
                        "，家庭住址是："+students[index].getAddress());
            }
            System.out.println("是否继续查询：");
            if(!sc.nextBoolean()){
                break;
            }
        }
    }
    public static Student[] addContent(Student[] students){
        Student[] students1 = new Student[students.length+5];
        for (int i = 0; i < students.length; i++) {
            students1[i] = students[i];
        }
        return students1;
    }
}
